<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<link rel="stylesheet" type="text/css" href="easyui/themes/default/easyui.css" />
		<link rel="stylesheet" type="text/css" href="easyui/themes/icon.css">
		<script type="text/javascript" src="easyui/jquery.min.js"></script>
		<script type="text/javascript" src="easyui/jquery.easyui.min.js"></script>
		<script type="text/javascript" src="easyui/locale/easyui-lang-zh_CN.js"></script>
		<script src="js/echarts.min.js"></script>
		<script type="text/javascript">
			function pagerFilter(data){
				if (typeof data.length == 'number' && typeof data.splice == 'function'){	// is array
					data = {
						total: data.length,
						rows: data
					}
				}
				var dg = $(this);
				var opts = dg.datagrid('options');
				var pager = dg.datagrid('getPager');
				pager.pagination({
					onSelectPage:function(pageNum, pageSize){
						opts.pageNumber = pageNum;
						opts.pageSize = pageSize;
						pager.pagination('refresh',{
							pageNumber:pageNum,
							pageSize:pageSize
						});
						dg.datagrid('loadData',data);
					}
				});
				if (!data.originalRows){
					data.originalRows = (data.list);
				}
				var start = (opts.pageNumber-1)*parseInt(opts.pageSize);
				var end = start + parseInt(opts.pageSize);
				data.rows = (data.originalRows.slice(start, end));
				return data;
			}

			$(function(){
				$('#dg').datagrid({ loadFilter: pagerFilter }).datagrid({
					url: 'yujing/findByPage'  ,   //加载数据
					title:'预警信息',
					rownumbers:true,
					singleSelect:true,  //取消多选
					autoRowHeight:false,
					pagination:true,  //分页
					pageSize:10,
					fit:true, //防止水平抖动
					toolbar:'#tb',
					fitColumns: true,
					columns:[[
						// {checkbox:true,field:''},
						{field:'time',title:'检测日期',width:150,editor:'textbox'},
						{field:'name',title:'报警类型',width:150,editor:'textbox'},
						{field:'dengji',title:'报警程度',width:100,editor:'textbox'},
					]]
				});
			});
			var index= -1;
			//进入编辑状态
			function goUpdate(){
				//获取选中的行
				// $("#dg").datagrid("beginEdit",0);
				var row =$("#dg").datagrid("getSelected");
				if(row!=null){
					//将index对应的行关闭编辑状态
					$("#dg").datagrid("endEdit",index);
					//获取row对应的index
					 index=$("#dg").datagrid("getRowIndex",row);
					 $("#dg").datagrid("beginEdit",index);
				}else{
					alert("操作错误，请勾选要选中的行记录");
				}
			}
			//保存修改的数据
			function saveUpdate(){
				//获取选中的行
				var row =$("#dg").datagrid("getSelected");
				if(row!=null){
					//获取row对应的index
					 index=$("#dg").datagrid("getRowIndex",row);
					 //结束编辑状态
					 $("#dg").datagrid("endEdit",index)
					 //将修改的数据提交到服务器
					 console.log(row);
					var data = {
						"id":row.id,
						"kinds": row.kinds,
						"max": row.max,
						"min": row.min,
						"avg": row.avg,
						"sd": row.sd,
						"cv": row.cv
					};
					//将实体类转换为json
					var datas = JSON.stringify(data);
					$.ajax({
						url: 'putaoZyj/update',
						contentType: "application/json;charset=UTF-8",
						data: datas,
						dataType: "json",
						type: "post",
						success: function (data) {
							//alert("访问到这了" + datas);

							if (data.msg == 1) {
								alert("修改成功！");
								history.go(0);
							} else {
								alert("操作错误，请重新操作！");

							}
						}
					});
				}else{
				alert("操作错误，请勾选要选中的行记录");
				}
			}
			function removeit(){
				var row =$("#dg").datagrid("getSelected");
				if(row!=null){
					editIndex=$("#dg").datagrid("getRowIndex",row);
					if (editIndex == undefined){return}
					$('#dg').datagrid('cancelEdit', editIndex)
							.datagrid('deleteRow', editIndex);
					editIndex = undefined;
					var data = {"id":row.id};
					//将实体类转换为json
					var datas = JSON.stringify(data);
					$.ajax({
						url:'yujing/delete',
						contentType: "application/json;charset=UTF-8",
						data: datas,
						dataType: "json",
						type: "post",
						success:function (data) {
							if(data.msg==0){
								alert("操作出错，请重试");
							}else{
								alert("删除成功");
							}
						}
					});
				}else{
					alert("请勾选所要删除的行");
				}

			}
			var editIndex = undefined;
			function endEditing() {
				if (editIndex == undefined) {
					return true
				}
				if ($('#dg').datagrid('validateRow', editIndex)) {
					return true;
				} else {
					return false;
				}
			}
			function doSearch() {
				var kinds = $("#kinds").val();
				var kinds1 = {"kinds":kinds};
				var datas = JSON.stringify(kinds1);
				alert(datas);
				$.ajax({
					url: 'putaoZyj/findByKinds',
					contentType: "application/json;charset=UTF-8",
					data: datas,
					dataType: "json",
					type: "post",
					success: function (data) {
						console.log(data);
						if (data) {
							$("#dg").datagrid('loadData', data);
						}
					}
				});
			}
			function refresh() {
				$('#dg').datagrid('reload');//刷新数据
			}
		</script>
	</head>
	<body style="border: 0px">
		<div id="tb">
<!--			<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true"onclick="$('#w').window('open')">添加</a>-->
<!--			<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true" onclick="goUpdate()">修改</a>-->
<!--			<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:true" onclick="saveUpdate()">保存</a>-->
			<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="removeit()">删除</a>
			<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-refresh',plain:true" onclick="refresh()">刷新</a>
<!--			<label>种类：</label><input class="wu-text" id="kinds" style="width:100px">-->
<!--			<a href="javascript:void(0)"  class="easyui-linkbutton" iconCls="icon-search" onclick="doSearch()">开始检索</a>-->
		</div>
		<div id="w" class="easyui-window" title="Modal Window" data-options="modal:true,closed:true,iconCls:'icon-save'" style="width:500px;height:500px;padding:10px;">
			<iframe src="putaozyj.html" width="100%" height="100%"></iframe>
		</div>
		<table id="dg"></table>
	</body>
</html>
